package so.shanku.essential.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import aym.util.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.StringUtil;
import so.shanku.essential.view.ListViewNoScroll;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/9.
 */
public class UserOrderAdapter extends MyBaseAdapter {

	/**
	 * 设置tag  0：标记是订单   1：还是售后
	 */
   private int tag=0;

	public void setTag(int tag) {
		this.tag = tag;
	}

	/**
	 * 操作订单的点击监听
	 */
	private UserOrderItemCompountClickListener userOrderItemCompountClickListener;

	public void setUserOrderItemCompountClickListener(UserOrderItemCompountClickListener userOrderItemCompountClickListener) {
		this.userOrderItemCompountClickListener = userOrderItemCompountClickListener;
	}

	public interface UserOrderItemCompountClickListener {
		void userOrderItemCompountClick(int position, Constant.UserOrderItemType userOrderItemType, int childPosition);
	}

	public UserOrderAdapter(Context context) {
		super(context);
	}

	public UserOrderAdapter(Context context, List<? extends Map<String, ?>> datas) {
		super(context, datas);
	}


	@Override
	public View getView(final int i, View view, ViewGroup viewGroup) {
		ViewHolder viewHolder = null;
		if (view == null) {
			view = layoutInflater.inflate(R.layout.item_user_order, viewGroup, false);
			viewHolder = new ViewHolder(view);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}
		final int itemPosition = i;
		final JsonMap jsonMap = (JsonMap) datas.get(i);
		viewHolder.tv_shop_name.setText(jsonMap.getStringNoNull("VendorName"));
		viewHolder.tv_goods_allmoney.setText(StringUtil.getFormatMoneyWithSign(jsonMap.getStringNoNull("RealTotal")));
		viewHolder.tv_pay_style.setText(jsonMap.getStringNoNull("StrOrderStatus"));
		List<JsonMap<String, Object>> dataHLV = jsonMap.getList_JsonMap("OrderdetailList");
		viewHolder.listview_item_order.setAdapter(new ItemOrderAdapter(context, dataHLV));
		viewHolder.listview_item_order.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				if (userOrderItemCompountClickListener != null) {
					userOrderItemCompountClickListener.userOrderItemCompountClick(itemPosition, Constant.UserOrderItemType.SEE_PRODUCT,position);
				}
			}
		});

		JsonMap optionButtonMap = jsonMap.getJsonMap("ShowOrderButton");
//		IsCancel 取消按钮
//		IsPay 支付按钮
//		IsRefundMoney 申请退款
//		IsSeeLogistics 查看物流
//		IsCinfirmReceipt 确认收货
//		IsSalesReturn 申请退货
//		IsReturnGoods 退货
//		IsArefundDetails 退款详情

		if(tag==0) {
			viewHolder.tv_order_cancel.setVisibility(optionButtonMap.getBoolean("IsCancel", false) ? View.VISIBLE : View.GONE);
			viewHolder.tv_order_pay.setVisibility(optionButtonMap.getBoolean("IsPay", false) ? View.VISIBLE : View.GONE);
			viewHolder.tv_order_detail.setVisibility(optionButtonMap.getBoolean("IsDetail", false) ? View.VISIBLE : View.GONE);
			viewHolder.tv_fund_return.setVisibility(optionButtonMap.getBoolean("IsRefundMoney", false) ? View.VISIBLE : View.GONE);
			viewHolder.refund_detail_tv.setVisibility(optionButtonMap.getBoolean("IsArefundDetails", false) ? View.VISIBLE : View.GONE);

			viewHolder.tv_goods_wuliu.setVisibility(optionButtonMap.getBoolean("IsSeeLogistics", false) ? View.VISIBLE : View.GONE);
			viewHolder.tv_goods_receive_confirm.setVisibility(optionButtonMap.getBoolean("IsCinfirmReceipt", false) ? View.VISIBLE : View.GONE);
			//viewHolder.tv_goods_return.setVisibility(optionButtonMap.getBoolean("IsSalesReturn", false) ? View.VISIBLE : View.GONE);
		} else if (tag==1){
			boolean bool=optionButtonMap.getBoolean("IsSalesReturn", false) ;
			viewHolder.tv_goods_return.setEnabled(bool);
			viewHolder.tv_goods_return.setVisibility(View.VISIBLE);
			//viewHolder.tv_do_goods_return.setVisibility(optionButtonMap.getBoolean("IsReturnGoods", false) ? View.VISIBLE : View.GONE);
		} else if (tag == 2) {
			viewHolder.refund_detail_tv.setVisibility(optionButtonMap.getBoolean("IsArefundDetails", false) ? View.VISIBLE : View.GONE);
		}

		setViewTag(viewHolder, i);
		setClickListener(viewHolder);
		return view;
	}

	/**
	 * s设置点击事件
	 *
	 * @param viewHolder
	 */
	private void setClickListener(ViewHolder viewHolder) {

		viewHolder.tv_order_cancel.setOnClickListener(onClickListener);
		viewHolder.tv_order_pay.setOnClickListener(onClickListener);
		viewHolder.tv_order_detail.setOnClickListener(onClickListener);
		viewHolder.tv_fund_return.setOnClickListener(onClickListener);
		viewHolder.tv_goods_return.setOnClickListener(onClickListener);
		viewHolder.tv_goods_wuliu.setOnClickListener(onClickListener);
		viewHolder.tv_goods_receive_confirm.setOnClickListener(onClickListener);
		viewHolder.tv_do_goods_return.setOnClickListener(onClickListener);
		viewHolder.linear_goto_shop.setOnClickListener(onClickListener);
		viewHolder.refund_detail_tv.setOnClickListener(onClickListener);
		// viewHolder.listview_item_order.setOnClickListener(onClickListener);
	}

	private View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			int viewId = v.getId();
			Integer tag = (Integer) v.getTag();
			JsonMap jsonMap = (JsonMap) datas.get(tag);
			switch (viewId) {
				case R.id.tv_order_cancel:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.CANCEL_ORDER,-1);
					}
					break;
				case R.id.tv_order_pay:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.GOTO_PAY,-1);
					}
					break;

				case R.id.tv_fund_return:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.FUND_RETURN,-1);
					}
					break;
				case R.id.tv_goods_return:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.GOODS_RETURN,-1);
					}
					break;
				case R.id.tv_goods_wuliu:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.SEELogistics,-1);
					}
					break;
				case R.id.tv_goods_receive_confirm:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.ORDER_CONFIRM,-1);
					}
					break;
				case R.id.linear_goto_shop:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.SEE_BRAND,-1);
					}
					break;
				case R.id.tv_order_detail:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.SEE_ORDER_DETAIL,-1);
					}
					break;
				case R.id.tv_do_goods_return:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.DO_GOODS_RETURN,-1);
					}
					break;
				case R.id.refund_detail_tv:
					if (userOrderItemCompountClickListener != null) {
						userOrderItemCompountClickListener.userOrderItemCompountClick(tag, Constant.UserOrderItemType.REFUND_DETAIL,-1);
					}
					break;

				default:
			}


		}
	};

	/**
	 * 设置标记
	 *
	 * @param viewHolder
	 * @param position
	 */
	private void setViewTag(ViewHolder viewHolder, int position) {
		viewHolder.tv_order_cancel.setTag(position);
		viewHolder.tv_order_pay.setTag(position);
		viewHolder.tv_order_detail.setTag(position);
		viewHolder.tv_fund_return.setTag(position);
		viewHolder.tv_goods_return.setTag(position);
		viewHolder.tv_goods_wuliu.setTag(position);
		viewHolder.tv_goods_receive_confirm.setTag(position);
		viewHolder.tv_do_goods_return.setTag(position);
		viewHolder.linear_goto_shop.setTag(position);
		viewHolder.listview_item_order.setTag(position);
		viewHolder.refund_detail_tv.setTag(position);
	}

	private class ViewHolder {
		/**
		 * 店铺
		 */
		@ViewInject(R.id.linear_goto_shop)
		private LinearLayout linear_goto_shop;
		/**
		 * 总价
		 */
		@ViewInject(R.id.tv_goods_allmoney)
		private TextView tv_goods_allmoney;
		/**
		 * 查看物流
		 */
		@ViewInject(R.id.tv_goods_wuliu)
		private TextView tv_goods_wuliu;

		/**
		 * 查看详情
		 */
		@ViewInject(R.id.tv_order_detail)
		private TextView tv_order_detail;


		/**
		 * 确认收货
		 */
		@ViewInject(R.id.tv_goods_receive_confirm)
		private TextView tv_goods_receive_confirm;
		/**
		 * 取消订单
		 */
		@ViewInject(R.id.tv_order_cancel)
		private TextView tv_order_cancel;

		/**
		 * 去支付
		 */
		@ViewInject(R.id.tv_order_pay)
		private TextView tv_order_pay;

		/**
		 * 申请退款
		 */
		@ViewInject(R.id.tv_fund_return)
		private TextView tv_fund_return;
		/**
		 * 申请退货
		 */
		@ViewInject(R.id.tv_goods_return)
		private TextView tv_goods_return;

		/**
		 * 进行退货
		 */
		@ViewInject(R.id.tv_do_goods_return)
		private TextView tv_do_goods_return;


		/**
		 * 退款详情
		 */
		@ViewInject(R.id.refund_detail_tv)
		private TextView refund_detail_tv;

		/**
		 * 支付方式
		 */
		@ViewInject(R.id.tv_pay_style)
		private TextView tv_pay_style;
		/**
		 * 店铺名字
		 */
		@ViewInject(R.id.tv_shop_name)
		private TextView tv_shop_name;
		/**
		 * 商品信息
		 */
		@ViewInject(R.id.listview_item_order)
		private ListViewNoScroll listview_item_order;

		public ViewHolder(View view) {
			ViewUtils.inject(this, view);
		}
	}
}
